﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Wpf_Tool_ZC.util
{
    public class UtilString
    {
        /// <summary>
        /// 普通的字符串(本地默认编码) 转成 16进制的字符串 (例如:"ABC"-->"414243")
        /// </summary>
        /// <returns></returns>
        public static string Str2Hex(string _str)
        {
            string rst = string.Empty;
            byte[] bb = System.Text.Encoding.Default.GetBytes(_str);
            for (int i = 0; i < bb.Length; i++)
                rst += bb[i].ToString("X2");

            return rst;
        }

        /// <summary>
        /// 16进制的字符串 转成 16进制的byte数组 (例如:"414243"-->"0x41, 0x42, 0x43, ")(这个转换出来的结果 便于C++声明变量:“byte bytes[]={0x41, 0x42, 0x43};”)
        /// </summary>
        /// <param name="_str"></param>
        /// <returns></returns>
        public static string HexStr2ByteArray(string _str)
        {
            Console.WriteLine("_str.Length: " + _str.Length);
            if (_str.Length % 2 != 0)
            {
                Console.WriteLine("字符串长度为奇数: " + _str.Length);
                return null;
            }

            string strRst = string.Empty;
            int i1 = _str.Length / 2;
            for (int i = 0; i < i1; i++)
            {
                strRst += "0x" + _str.Substring(i * 2, 2) + ", ";
                if (i != (i1 - 1))
                    if (((i + 1) % 16) == 0)
                        strRst += "\r\n";
                    else
                    {
                        if (((i + 1) % 8) == 0)
                            strRst += " ";
                    }
            }

            return strRst;
        }


        public static string HexStr2ByteArray_01(string _str, int _iSkipCount, int _iCountPerRow)
        {
            Console.WriteLine("_str.Length: " + _str.Length);
            if (_str.Length % 2 != 0)
            {
                Console.WriteLine("字符串长度为奇数: " + _str.Length);
                return null;
            }
            if (_iSkipCount >= _iCountPerRow)
            {
                Console.WriteLine("_iSkipCount >= _iCountPerRow : {0}, {1}", _iSkipCount, _iCountPerRow);
                return null;
            }

            string strRst = string.Empty;
            int iIdxInRow = 0;
            for (int i = 0; i < _iSkipCount; i++)
            {
                if (i % 2 == 0)
                    strRst += "     ";
                else
                    strRst += "      ";
            }
            iIdxInRow = _iSkipCount;

            int i1 = _str.Length / 2;
            for (int i = 0; i < i1; i++)
            {
                if (iIdxInRow % 2 == 0)
                    strRst += "0x" + _str.Substring(i * 2, 2) + ",";
                else
                    strRst += "0x" + _str.Substring(i * 2, 2) + ", ";

                iIdxInRow++;
                if (iIdxInRow >= _iCountPerRow)
                {
                    strRst += "\r\n";
                    iIdxInRow = 0;
                }
            }

            return strRst;
        }

        /// <summary>
        /// 由 普通的字符串(本地默认编码) 得到 UTF-8编码的像Base64编码的字符串 (例如:"茜茜"-->"%E8%8C%9C%E8%8C%9C")
        /// </summary>
        /// <param name="_str"></param>
        /// <returns></returns>
        public static string EncodeZ_Utf8(string _str)
        {
            string rst = string.Empty;

            // 步骤1:
            UTF8Encoding utf8 = new UTF8Encoding();
            byte[] encodedBytes = utf8.GetBytes(_str);

            // 步骤2:
            for (int i = 0; i < encodedBytes.Length; i++)
                rst += "%" + encodedBytes[i].ToString("X2");
            return rst;
        }

        /// <summary>
        /// EncodeZ_Utf8(...) 过程的逆操作
        /// </summary>
        /// <param name="_str"></param>
        /// <returns></returns>
        public static string DecodeZ_Utf8(string _str)
        {
            if (_str.Length % 3 != 0)
            {
                Console.WriteLine("字符串长度不是3的整数: " + _str.Length);
                return null;
            }

            int i1 = _str.Length / 3;
            byte[] bytes = new byte[i1];
            for (int i = 0; i < i1; i++)
            {
                string str = "" + _str[3 * i + 1] + _str[3 * i + 2];
                int ii = Convert.ToInt32(str, 16);
                bytes[i] = (byte)ii;
            }

            return System.Text.Encoding.UTF8.GetString(bytes);
            //return null;
        }

        /// <summary>
        /// C#提供的 Base64编码
        /// </summary>
        /// <param name="_str"></param>
        /// <returns></returns>
        public static string Base64_Encode(string _str)
        {
            byte[] bytes = Encoding.Default.GetBytes(_str);
            return Convert.ToBase64String(bytes);
            //return _str;
        }

        /// <summary>
        /// C#提供的 Base64编码 的解码
        /// </summary>
        /// <param name="_str"></param>
        /// <returns></returns>
        public static string Base_Decode(string _str)
        {
            byte[] outputb = Convert.FromBase64String(_str);
            return Encoding.Default.GetString(outputb);
            //return _str;
        }

    }
}
